/*
 * @lc app=leetcode.cn id=543 lang=javascript
 *
 * [543] 二叉树的直径
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var diameterOfBinaryTree = function(root) {
    let max = 0
    const dfs = function(node) {
        if (!node) return 0
        let l = dfs(node.left) + 1
        let r = dfs(node.right) + 1
        max = Math.max(max, l+r-2)
        return Math.max(l, r)
    }
    dfs(root)
    console.log(max)
    return max
};
// @lc code=end

function TreeNode(val) {
    this.val = val;
    this.left = this.right = null;
}
let root = new TreeNode(1)
root.left = new TreeNode(2)
root.right = new TreeNode(3)
root.left.left = new TreeNode(4)
root.left.right = new TreeNode(5)
root.left.right.left = new TreeNode(6)

let ans = diameterOfBinaryTree(root)